package com.example.administrator.smartdevice.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.example.administrator.smartdevice.R;
import com.example.administrator.smartdevice.base.BaseActivity;
import com.example.administrator.smartdevice.event.MessageEvent;
import com.example.administrator.smartdevice.fragment.AskListFragment;
import com.example.administrator.smartdevice.fragment.DeviceListFragment;
import com.example.administrator.smartdevice.net.HttpUtils;
import com.example.administrator.smartdevice.serial.SerialPortManager;
import com.example.administrator.smartdevice.utils.AgentConstants;
import com.example.administrator.smartdevice.utils.AgentSharedPreferences;
import com.hhc.netty.DeviceModel;
import com.hhc.netty.NettyClient;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class DeviceActivity extends BaseActivity {
    @BindView(R.id.activity_device_viewpager)
    protected ViewPager activity_device_viewpager;

    @BindView(R.id.activity_device_list_tab)
    protected TabLayout activity_device_list_tab;

    private FragmentPagerAdapter fragmentPagerAdapter;
    private List<Fragment> fragmentList;
    private List<String> titleList;
    private int state = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_device);
    }

    @Override
    protected void initData() {
        initCommonHeader();
        initFragmentList();
        initViewPager();
    }

    @Override
    protected void onFailure(int requestCode, Object object) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object) {
        if (object != null) {
            switch (requestCode) {
//                case 5001:
//                    if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_OK)) {
//                        //1.存在,本地的状态list更新，同时发送上线通知
//                        //10:上线 20：下线
//                        if (messageEvent[1].equals("10")) {
//                            DeviceModel deviceModel = new DeviceModel();
//                            //基本项：device_id和appliance_id
//                            deviceModel.setApplianceId(AgentSharedPreferences.getMemberInfo(this).getMemberDeviceId());
//                            deviceModel.setApplianceId(messageEvent[0]);
//                            //家电上线状态:上线
//                            deviceModel.setOnlineStatus("10");
//                            //家电开关状态：关
//                            deviceModel.setThreadStatus("20");
//                            NettyClient.getInstance().deviceOnline(deviceModel);
//                        }else if(messageEvent[1].equals("20")){
//                            DeviceModel deviceModel = new DeviceModel();
//                            //基本项：device_id和appliance_id
//                            deviceModel.setApplianceId(AgentSharedPreferences.getMemberInfo(this).getMemberDeviceId());
//                            deviceModel.setApplianceId(messageEvent[0]);
//                            //家电上线状态：下线
//                            deviceModel.setOnlineStatus("20");
//                            //家电开关状态：关
//                            deviceModel.setThreadStatus("20");
//                            //本地存设备状态->用到场景：移动端请求获取设备状态
//                            NettyClient.getInstance().deviceOffline(deviceModel);
//                        }
//                    } else {
//                        //2.不存在，直接放到请求列表，即是请求加入
//
//                    }
//                    break;
                default:
                    break;
            }
        }
    }

    protected void initCommonHeader() {
        configCommonHeader(this, "设备管理", true);
    }

    protected void initViewPager() {
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return fragmentList.get(arg0);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titleList.get(position);
            }
        };

        activity_device_viewpager.setAdapter(fragmentPagerAdapter);
        activity_device_viewpager.setOffscreenPageLimit(2);
        activity_device_viewpager.setCurrentItem(state);
        activity_device_list_tab.setupWithViewPager(activity_device_viewpager);
    }

    protected void initFragmentList() {
        fragmentList = new ArrayList<>();
        fragmentList.add(DeviceListFragment.newInstance());
        fragmentList.add(AskListFragment.newInstance());

        titleList = new ArrayList<>();
        titleList.add("设备列表");
        titleList.add("请求列表");
    }

    @OnClick(R.id.common_head_more)
    protected void test() {
        DeviceModel deviceModel = new DeviceModel();
        //基本项：device_id和appliance_id
        deviceModel.setDeviceId(AgentSharedPreferences.getMemberInfo(this).getMemberDeviceId());
        deviceModel.setApplianceId("000001");
        //家电上线状态:上线
        deviceModel.setOnlineStatus("10");
        //家电开关状态：关
        deviceModel.setStatus("20");
        NettyClient.getInstance().deviceOnline(deviceModel);

        DeviceModel deviceModel1 = new DeviceModel();
        //基本项：device_id和appliance_id
        deviceModel1.setDeviceId(AgentSharedPreferences.getMemberInfo(this).getMemberDeviceId());
        deviceModel1.setApplianceId("000002");
        //家电上线状态:上线
        deviceModel1.setOnlineStatus("10");
        //家电开关状态：关
        deviceModel1.setStatus("20");
        NettyClient.getInstance().deviceOnline(deviceModel1);
    }
}
